#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QProcess>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_boardComboBox_currentTextChanged(const QString &arg1);
    void on_socComboBox_currentTextChanged(const QString &arg1);
    void on_burnAllPushButton_clicked();
    void on_burnIFlashPushButton_clicked();
    void on_burnEFlashPushButton_clicked();
    void on_eraseIFlashPushButton_clicked();
    void on_eraseEFlashPushButton_clicked();
    void on_bootComboBox_currentTextChanged(const QString &arg1);
    void on_appComboBox_currentTextChanged(const QString &arg1);
    void on_openBootPushButton_clicked();
    void on_openAppPushButton_clicked();
    void on_clearCountPushButton_clicked();

private:
    Ui::MainWindow *ui;
    QString configFilePath = "configs.ini";
    bool factoryMode = false;
    QString curSection;
    QString curSoc;
    QString curExtLoader;
    QString curBoot;
    QString curApp;
    QString curBkpPara;
    QString defaultBkpPara = "bakp_1_para_0x000fe000.bin";
    QString curUpdateFlag = "flag_update_0x000fc000.bin";
    QString curNeedUpdateFlag = "flag_need_update_0x000fb000.bin";
    QString firmwareDir = "fw\\";
    QString extLoaderDir = "ST-LINK Utility\\ExternalLoader\\";
    QString stlinkPath = "ST-LINK Utility\\ST-LINK_CLI.exe";
    QString boardImagePath = "images\\";
    QString defaultIFlashBootOffset = "0x08000000";
    QString defaultIFlashAppOffset = "0x08010000";
    QString defaultExtFlashBkpParaOffset = "0x000fe000";
    QString defaultExtFlashAppOffset = "0x00200000";
    QString defaultExtFlashNeedUpdateFlagOffset = "0x000fb000";
    QString defaultExtFlashUpdateFlagOffset = "0x000fc000";

    QProcess cliProcessInternal;
    QProcess cliProcessExternal;

    QString findSectionByName(const QString &boardName); // 根据name字段查找Board段名
    QString findKeyByValue(const QString &section, const QString &value); // 根据键值查找键名，可能存在冲突，慎用
    int burnExternFlash(QString filePath, QString loaderPath, QString offset);
    int eraseExternFlash(QString loaderPath);
    int burnInternalFlash(QString filePath, QString offset);
    int eraseInternalFlash();
    int judgeConnectionError(QString res); // 根据stlink输出的内容，判断是否连接成功
    qint64 convertQIntEndian(qint64 src); // 转换64位字节序
    qint32 convertQIntEndian(qint32 src); // 转换32位字节序
};

#endif // MAINWINDOW_H
